package org.scohen.juploadr.uploadapi.flickrrest.photosets;

import java.util.SortedMap;
import java.util.TreeMap;

import org.scohen.juploadr.app.PhotoSet;
import org.scohen.juploadr.app.UploadImage;
import org.scohen.juploadr.uploadapi.ProtocolException;
import org.scohen.juploadr.uploadapi.flickrrest.DefaultFlickrHandler;
import org.scohen.juploadr.uploadapi.flickrrest.FlickrMethod;
import org.scohen.juploadr.uploadapi.flickrrest.PhotoNotFoundException;
import org.scohen.juploadr.uploadapi.flickrrest.PostUploadAction;
import org.scohen.juploadr.uploadapi.flickrrest.RestFlickrApi;

public class AddPhoto extends FlickrMethod implements PostUploadAction {
    private PhotoSet toAddTo;
    private String photoId;
    private UploadImage image;

    public AddPhoto(PhotoSet toAddTo, UploadImage img) {
        this.toAddTo = toAddTo;
        this.image = img;
        this.photoId = img.getPhotoId();

    }

    public DefaultFlickrHandler getResponseHandler() {
        return new AddPhotoHandler(this);
    }

    public SortedMap<String, String> getParams() {
        SortedMap<String, String> params = new TreeMap<String, String>();
        params.put("method", "flickr.photosets.addPhoto");
        params.put("api_key", RestFlickrApi.FLICKR_API_KEY);
        params.put("photoset_id", toAddTo.getId());
        params.put("photo_id", photoId);
        return params;
    }

    public boolean isAuthorized() {
        return true;
    }

    public String getErrorText() {
        return "add " + image.getTitle() + " to photoset " + toAddTo.getTitle();
    }

    @Override
    protected ProtocolException buildProtocolException(int errorCode) {
        switch (errorCode) {
        case 1:
            return new PhotosetNotFoundException();
        case 2:
            return new PhotoNotFoundException();
        case 3:
            return new PhotoAlreadyInSetException();
        default:
            return null;
        }
    }

}
